//
//  SpinnerView.m
//  HSBCHybrid
//
//  Created by Ray Chum on 6/10/13.
//
//

#import "SpinnerView.h"

@implementation SpinnerView

- (id)initWithFrame:(CGRect)frame LargeSize:(BOOL)large AlignToCenter:(BOOL)center;
{
    NSArray *images;
    NSBundle *bundle = [NSBundle mainBundle];
    images = [NSArray arrayWithObjects:
              [UIImage imageWithContentsOfFile:[bundle pathForResource:@"SpinnerFrame1.png" ofType:nil]],
              [UIImage imageWithContentsOfFile:[bundle pathForResource:@"SpinnerFrame2.png" ofType:nil]],
              [UIImage imageWithContentsOfFile:[bundle pathForResource:@"SpinnerFrame3.png" ofType:nil]],
              [UIImage imageWithContentsOfFile:[bundle pathForResource:@"SpinnerFrame4.png" ofType:nil]],
              [UIImage imageWithContentsOfFile:[bundle pathForResource:@"SpinnerFrame5.png" ofType:nil]],
              [UIImage imageWithContentsOfFile:[bundle pathForResource:@"SpinnerFrame6.png" ofType:nil]],
              [UIImage imageWithContentsOfFile:[bundle pathForResource:@"SpinnerFrame7.png" ofType:nil]],
              [UIImage imageWithContentsOfFile:[bundle pathForResource:@"SpinnerFrame8.png" ofType:nil]],
              nil];
    if (center){
        self = [super initWithFrame:CGRectMake(frame.size.width/2 - 44/2, frame.size.height/2 - 44/2, 44, 44)];
    } else {
        self = [super initWithFrame:CGRectMake(frame.size.width/2 - 32/2, frame.size.height-60, 32,32)];
    }
    
    if (self) {
        self.animationImages = images;
        self.animationDuration = 1;
        self.backgroundColor = [UIColor clearColor];
        self.autoresizingMask =
        UIViewAutoresizingFlexibleRightMargin |
        UIViewAutoresizingFlexibleLeftMargin |
        UIViewAutoresizingFlexibleBottomMargin |
        UIViewAutoresizingFlexibleTopMargin;
    }
    return self;
}

- (void)startAnimating
{
    [self setHidden:NO];
    [super startAnimating];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

- (void)stopAnimating
{
    [self setHidden:YES];
    [super stopAnimating];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

@end
